<?php

define('ACTION_CHANGEGROUPNAME', 'name');
define('ACTION_ADDPLACE', 'addp');
define('ACTION_DELETEPLACE', 'delp');
define('ACTION_ADDFRIEND', 'addf');
define('ACTION_DELETEFRIEND', 'delf');
define('ACTION_ACCEPTINVITATION', 'accept');
define('ACTION_REJECTINTITATION', 'reject');
define('ACTION_DELETEPLACE', 'delp');
define('ACTION_ADDCOMMENT', 'addc');
define('ACTION_GETINFOGROUP', 'getg');


switch(true){
	case (strcmp(ACTION_CHANGEGROUPNAME, $action) == 0):
		if(isset($_GET['gid']) && !empty($_GET['name'])){
			$idGroup = $_GET['gid'];
			$newGroupName = $_GET['name'];
			
			$group = new Group();
			if($group->loadFromDB($idGroup)){
				$group->changeName($newGroupName);
				$result = array('status'=> NO_ERR);
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_ADDPLACE, $action) == 0):
		if(isset($_GET['gid']) && isset($_GET['pid'])){
			$idGroup = $_GET['gid'];
			$idNewPlace = $_GET['pid'];
			
			$group = new Group();
			$newPlace = new Place();
			if($group->loadFromDB($idGroup)){ //Carga y comprueba el grupo
				if($newPlace->loadFromDB($idNewPlace)){ //Carga y comprueba el lugar.
					$user->addPlace2Group($idNewPlace, $idGroup);
					$result = array('status'=> NO_ERR);
				} else {
					$result = array('status' => ERR_PLACE_NOTEXISTS);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;		
	case (strcmp(ACTION_DELETEPLACE, $action) == 0):
		if(isset($_GET['gid']) && isset($_GET['pid'])){
			$idGroup = $_GET['gid'];
			$idNewPlace = $_GET['pid'];
			
			$group = new Group();
			$newPlace = new Place();
			if($group->loadFromDB($idGroup)){ //Carga y comprueba el grupo
				if($newPlace->loadFromDB($idNewPlace)){ //Carga y comprueba el lugar.
					$user->deletePlace4Group($idNewPlace, $idGroup);	
					$result = array('status'=> NO_ERR);
				} else {
					$result = array('status' => ERR_PLACE_NOTEXISTS);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_ADDFRIEND, $action) == 0):
		if(isset($_GET['gid']) && isset($_GET['fid'])){
			$idGroup = $_GET['gid'];
			$idFriend = $_GET['fid'];
			
			$group = new Group();
			$friend = new User();
			if($group->loadFromDB($idGroup)){ //Carga y comprueba el grupo
				if($friend->load($idFriend)){ //Carga y comprueba el usuario.
					if($user->hasFriend($idFriend)){
						if($user->manageGroup($idGroup)){
							$user->addFriend2Group($idFriend, $idGroup);
							$result = array('status'=> NO_ERR);
						} else {
							$result = array('status' => ERR_USER_NOMANAGE_GROUP);
						}
					} else {
						$result = array('status' => ERR_FRIEND_NOTEXISTS);
					}
				} else {
					$result = array('status' => ERR_USER_NOTEXISTS);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_DELETEFRIEND, $action) == 0):
		if(isset($_GET['gid']) && isset($_GET['fid'])){
			$idGroup = $_GET['gid'];
			$idFriend = $_GET['fid'];
			
			$group = new Group();
			$friend = new User();
			if($group->loadFromDB($idGroup)){ //Carga y comprueba el grupo
				if($friend->load($idFriend)){ //Carga y comprueba el usuario.
					if($user->hasFriend($idFriend)){
						if($user->manageGroup($idGroup)){
							$user->delFriend4Group($idFriend, $idGroup);
							$result = array('status'=> NO_ERR);
						} else {
							$result = array('status' => ERR_USER_NOMANAGE_GROUP);
						}
					} else {
						$result = array('status' => ERR_FRIEND_NOTEXISTS);
					}
				} else {
					$result = array('status' => ERR_USER_NOTEXISTS);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_ACCEPTINVITATION, $action) == 0):
		if(isset($_GET['gid'])){
			$idGroup = $_GET['gid'];
			
			$group = new Group();
			if($user->putStateInvitation4Group($idGroup, 'acept')){
				$result = array('status'=> NO_ERR);
			} else {
				$result = array('status' => ERR_HASNO_INVITATION);				
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_REJECTINTITATION, $action) == 0):
		if(isset($_GET['gid'])){
			$idGroup = $_GET['gid'];
			
			$group = new Group();
			if($user->putStateInvitation4Group($idGroup, 'reject')){
				$result = array('status'=> NO_ERR);
			} else {
				$result = array('status' => ERR_HASNO_INVITATION);				
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_ADDCOMMENT, $action) == 0):
		if(isset($_GET['gid']) && isset($_POST['msg'])){
			$idGroup = $_GET['gid'];
			$idPlace = $_GET['pid'];
			$msg = $_POST['msg'];
			
			$group = new Group();
			$friend = new User();
			$newPlace = new Place();
			if($group->loadFromDB($idGroup)){
				if((isset($_GET['pid']) && $newPlace->loadFromDB($idPlace)) || !isset($_GET['pid'])){
					$comment = $user->addComment($idGroup, $idPlace, to_utf8($msg));
					$result = array('status'=> NO_ERR ,'result' => $comment->tojson());	
				} else {
					$result = array('status' => ERR_PLACE_NOTEXISTS);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);				
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);
		}
		break;
	case (strcmp(ACTION_GETINFOGROUP, $action) == 0):
		if(isset($_GET['gid'])){
			$idGroup = $_GET['gid'];
			$group = new Group();			
			if($group->loadFromDB($idGroup)){
				if($user->isInvitedInGroup($idGroup)){
					$group->getInfoGroup();
					$result = array('status'=> NO_ERR ,'result' => $group->tojson());
				} else {
					$result = array('status' => ERR_NOTINGROUP);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}
		break;
	default:
		$result = array('status' => ERR_GENERAL);
}

?>